/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmLogin_RawatInap.java
 *
 * Created on Jan 30, 2012, 1:42:20 PM
 */
package SIMRS.RawatInap.view;

import SIMRS.RawatInap.sql.koneksiDatabase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import usu.widget.Form;

/**
 *
 * @author MAULIDA
 */
public class frmLogin_RawatInap extends Form {

    /** Creates new form frmLogin_RawatInap */
    public frmLogin_RawatInap() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label1 = new SIMRS.RawatInap.widget.Label();
        label2 = new SIMRS.RawatInap.widget.Label();
        userRawatInap = new SIMRS.RawatInap.widget.TextBox();
        passwordRawatInap = new SIMRS.RawatInap.widget.PasswordBox();
        btnLogin = new SIMRS.RawatInap.widget.ButtonBig();

        label1.setText("Username:");

        label2.setText("Password:");

        userRawatInap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userRawatInapActionPerformed(evt);
            }
        });

        passwordRawatInap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordRawatInapActionPerformed(evt);
            }
        });

        btnLogin.setBackground(new java.awt.Color(102, 0, 102));
        btnLogin.setForeground(new java.awt.Color(102, 0, 102));
        btnLogin.setText("Log in");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(passwordRawatInap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(userRawatInap, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(130, 130, 130)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(166, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(userRawatInap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordRawatInap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(150, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-428)/2, (screenSize.height-356)/2, 428, 356);
    }// </editor-fold>//GEN-END:initComponents

    private void userRawatInapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userRawatInapActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_userRawatInapActionPerformed

    private void passwordRawatInapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordRawatInapActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_passwordRawatInapActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:
         if(!(userRawatInap.getText().equals("") && passwordRawatInap.getText().equals(""))){
            try {
                Connection connection = koneksiDatabase.getKoneksi();
                String sql = "SELECT * FROM user WHERE "
                        + "username = '"+userRawatInap.getText()+"' "
                        + "and password = MD5('"+passwordRawatInap.getText()+"')";
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery(sql);
                if(rs.next()){
                    JOptionPane.showMessageDialog(this, "Selamat Datang : "+rs.getString("nama"));                    
                    setVisible(false);
                    frmUtama u = new frmUtama();
                    u.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(this, "Maaf Username dan Password yang Anda masukkan salah!!");
                }
                
            } catch (SQLException ex) {
                System.out.println("Terjadi kesalahan dalam proses login");
                ex.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Anda harus memasukkan Username dan Password!!!");
        }
        
    }//GEN-LAST:event_btnLoginActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private SIMRS.RawatInap.widget.ButtonBig btnLogin;
    private SIMRS.RawatInap.widget.Label label1;
    private SIMRS.RawatInap.widget.Label label2;
    private SIMRS.RawatInap.widget.PasswordBox passwordRawatInap;
    private SIMRS.RawatInap.widget.TextBox userRawatInap;
    // End of variables declaration//GEN-END:variables
}
